/**
 * Copyright (c) 2019 AsHacker All rights reserved.
 * <p>
 * http://www.ashacker.com
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;

import com.alibaba.fastjson.JSON;
import io.renren.common.utils.R;
import io.renren.common.validator.Assert;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.constants.StringConstants;
import io.renren.modules.app.form.out.AppSysNoticeForm;
import io.renren.modules.lucky.entity.AppSysAgreementEntity;
import io.renren.modules.lucky.entity.AppSysFeedbackEntity;
import io.renren.modules.lucky.entity.AppSysVersionEntity;
import io.renren.modules.lucky.service.*;
import io.renren.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * APP系统信息
 *
 * @author Chen chenchen@ashacker.com
 */
@RestController
@RequestMapping("/app")
@Api("APP系统信息")
public class AppSystemController extends AbstractController {

    @Autowired
    private AppSysAgreementService appSysAgreementService;

    @Autowired
    private AppSysVersionService appSysVersionService;

    @Autowired
    private AppSysNoticeService appSysNoticeService;

    @Autowired
    private AppSysFeedbackService appSysFeedbackService;

    @Autowired
    private AppGoodsAddrService appGoodsAddrService;

    /**
     * 活动规则
     */
    @GetMapping("getAgreement")
    @ApiOperation("活动规则")
    public R getAgreement(@RequestParam String agreement_type) {

        logger.info("getAgreement方法执行开始：传入参数为   " + agreement_type);

        String agreementType = agreement_type == null ? "" : agreement_type;

        // 获取协议信息
        AppSysAgreementEntity agreement = appSysAgreementService.queryByAgreementType(agreementType);

        Map<String, Object> map = new HashMap<>();
        map.put("agreement", agreement);

        logger.info("getAgreement方法执行结束：返回值为   " + JSON.toJSONString(map));

        return R.ok(map);
    }

    /**
     * 下载更新app
     */
    @PostMapping("downloadApp")
    @ApiOperation("下载更新app")
    public R downloadApp(@RequestParam String version_num) {

        logger.info("downloadApp方法执行开始：传入参数为   " + version_num);

        String versionNum = version_num == null ? "" : version_num;

        // 获取最新的app版本信息
        AppSysVersionEntity version = appSysVersionService.queryByVersionNum(versionNum);

        Map<String, Object> map = new HashMap<>();
        // map.put("expire", jwtUtils.getExpire());
        map.put("version", version);

        logger.info("downloadApp方法执行结束：返回值为   " + JSON.toJSONString(map));

        return R.ok(map);
    }

    /**
     * 公告消息列表(没有传递user_id，则认为是全部消息)
     */
    @Login
    @GetMapping("getNoticeList")
    @ApiOperation("公告消息")
    public R getNoticeList(@RequestParam String user_id, @RequestParam String page, @RequestParam String limit,
                           @RequestParam String token) {

        logger.info("getNoticeList方法执行开始：传入参数为   " + user_id + "---" + page + "---" + limit);

        String userId = user_id == null ? "" : user_id;
        String pageIndex = page == null ? "" : page;
        String pageNum = limit == null ? "" : limit;

        // 获取公告消息列表
        List<AppSysNoticeForm> list = null;
        try {
            list = appSysNoticeService.queryNoticeList(userId, pageIndex, pageNum);
        } catch (Exception e) {
            logger.error("保存信息失败,错误原因:" + e.getMessage(), e);
            e.printStackTrace();
        }

        Map<String, Object> map = new HashMap<>();
        map.put("list", list);

        logger.info("getNoticeList方法执行结束：返回值为   " + JSON.toJSONString(map));

        return R.ok(map);
    }

    /**
     * 更改消息公告
     */
    @Login
    @PostMapping("updateNotice")
    @ApiOperation("更改消息")
    public R updateNotice(@RequestParam String notice_id, @RequestParam String token, @RequestParam String user_id) {

        logger.info("updateNotice方法执行开始：传入参数为   " + notice_id + "---" + token + "---" + user_id);

        String noticeId = notice_id == null ? "" : notice_id;
        String userId = user_id == null ? "" : user_id;

        Map<String, Object> map = new HashMap<>();
        boolean boo = appSysNoticeService.updateNotice(noticeId, token, userId);
        if (boo)
            map.put("flag", 1);    //操作成功

        logger.info("updateNotice方法执行结束：返回值为   " + JSON.toJSONString(map));

        return R.ok(map);
    }

    /**
     * 调控中奖策略接口
     * 		如果flag为0 则执行系统定时任务
     * 		如果flag为1 则启动指定userId获奖
     */
    @Login
    @PostMapping("changeWinnerFlag")
    @ApiOperation("改变中奖策略")
    public R changeWinnerFlag(@RequestParam String flag_id, @RequestParam String token) {

        logger.info("changeWinnerFlag方法执行开始：传入参数为   " + flag_id + "---" + token);

        String FlagId = flag_id == null ? "0" : flag_id;

        // 更改中奖策略
        StringConstants.winnerFlag = Integer.parseInt(FlagId);

        Map<String, Object> map = new HashMap<>();
        map.put("winnerFlag", StringConstants.winnerFlag);

        logger.info("changeWinnerFlag方法执行结束：返回值为   " + JSON.toJSONString(map));

        return R.ok(map);
    }

    /**
     * 选择中奖用户接口
     * 		当flag为1的时候 可以选择指定用户id获奖
     */
    @Login
    @PostMapping("chooseWinner")
    @ApiOperation("选择中奖用户")
    public R chooseWinner(@RequestParam String user_id, @RequestParam String goods_id, @RequestParam String token) {

        logger.info("chooseWinner方法执行开始：传入参数为   " + user_id + "---" + goods_id + "---" + token);

        String userId = user_id == null ? "0" : user_id;
        String goodsId = goods_id == null ? "0" : goods_id;

        // 选择中奖用户
        String str = appGoodsAddrService.chooseWinner(userId, goodsId);

        Map<String, Object> map = new HashMap<>();
        map.put("str", str);

        logger.info("chooseWinner方法执行结束：返回值为   " + JSON.toJSONString(map));

        return R.ok(map);
    }

    @Login
    @PostMapping("addFeedback")
    @ApiOperation("增加反馈")
    public R addFeedback(@RequestBody AppSysFeedbackEntity feedback, @RequestParam String token) {

        logger.info("addFeedback方法执行开始：传入参数为   " + JSON.toJSONString(feedback) + "---" + token);

        //表单校验
        Assert.isNull(feedback, "反馈不可为空");

        Map<String, Object> map = appSysFeedbackService.addFeedback(feedback);

        logger.info("addFeedback方法执行结束：返回值为   " + JSON.toJSONString(map));

        return R.ok(map);
    }

}
